package org.dromara.oce.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.dromara.common.core.enums.DimensionsEnum;
import org.dromara.common.core.utils.ObjectUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.oce.domain.OceAdvertiserDataAnalysis;
import org.dromara.oce.domain.OceAdvertiserReferralRelation;
import org.dromara.oce.domain.dto.GetCustomReportDTO;
import org.dromara.oce.domain.req.OceAdvertiserDataAnalysisQo;
import org.dromara.oce.domain.vo.OceAdvertiserDataAnalysisVo;
import org.dromara.oce.mapper.OceAdvertiserDataAnalysisMapper;
import org.dromara.oce.service.OceAdvertiserDataAnalysisService;
import org.dromara.oce.service.OceAdvertiserReferralRelationService;
import org.dromara.oce.service.remote.OceanEngineRemoteService;
import org.dromara.oce.utils.DateTimeUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @desc:接口实现层
 * @autor: hhl
 * @date: 2025-05-19 15:28:23<br>
 */
@Service
@AllArgsConstructor
public class OceAdvertiserDataAnalysisServiceImpl extends ServiceImpl<OceAdvertiserDataAnalysisMapper, OceAdvertiserDataAnalysis> implements OceAdvertiserDataAnalysisService {

    private final OceanEngineRemoteService oceanEngineRemoteService;
    private final OceAdvertiserReferralRelationService oceAdvertiserReferralRelationService;


    @Override
    public TableDataInfo<OceAdvertiserDataAnalysisVo> queryPageList(OceAdvertiserDataAnalysisQo qo, PageQuery pageQuery) {


        return null;
    }

    @Override
    public List<OceAdvertiserDataAnalysisVo> queryList(OceAdvertiserDataAnalysisQo qo) {
        LambdaQueryWrapper<OceAdvertiserDataAnalysis> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.gt(OceAdvertiserDataAnalysis::getStatCost, 0L);
//        lambdaQueryWrapper.eq(OceAdvertiserDataAnalysis::getThirdAdvertiserId,1820646562618380L);
        lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(qo.getStartDate()), OceAdvertiserDataAnalysis::getThatdayTimestamp, DateTimeUtil.getTimestampSecond(qo.getStartDate()));
        lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(qo.getDramaTitle()),OceAdvertiserDataAnalysis::getDramaTitle,qo.getDramaTitle());
//        lambdaQueryWrapper.last("limit 10");
        return this.baseMapper.selectVoList(lambdaQueryWrapper, OceAdvertiserDataAnalysisVo.class);
    }

    @Override
    public void syncOceDataAnalysis(List<OceAdvertiserReferralRelation> batchList, String date, Long dateTimestamp) {
        List<String> thirdAdvertiserIdList = batchList.stream().map(OceAdvertiserReferralRelation::getThirdAdvertiserId).collect(Collectors.toList());

        this.remove(new LambdaQueryWrapper<OceAdvertiserDataAnalysis>().in(OceAdvertiserDataAnalysis::getThirdAdvertiserId, thirdAdvertiserIdList)
            .eq(OceAdvertiserDataAnalysis::getThatdayTimestamp, DateTimeUtil.getTimestampSecond(date)));

        List<OceAdvertiserDataAnalysis> addList = new ArrayList<>();
        for (OceAdvertiserReferralRelation referralRelation : batchList) {
            List<GetCustomReportDTO> list1 = oceanEngineRemoteService.getCustomReport(referralRelation.getThirdAdvertiserId(), referralRelation.getThirdAdvertiserAppCode(),
                date, date, DimensionsEnum.GENDER);

            List<GetCustomReportDTO> list2 = oceanEngineRemoteService.getCustomReport(referralRelation.getThirdAdvertiserId(), referralRelation.getThirdAdvertiserAppCode(),
                date, date, DimensionsEnum.AGE);

            List<GetCustomReportDTO> list3 = oceanEngineRemoteService.getCustomReport(referralRelation.getThirdAdvertiserId(), referralRelation.getThirdAdvertiserAppCode(),
                date, date, DimensionsEnum.AGE_V2);

            List<GetCustomReportDTO> list4 = oceanEngineRemoteService.getCustomReport(referralRelation.getThirdAdvertiserId(), referralRelation.getThirdAdvertiserAppCode(),
                date, date, DimensionsEnum.AC);

            List<GetCustomReportDTO> list5 = oceanEngineRemoteService.getCustomReport(referralRelation.getThirdAdvertiserId(), referralRelation.getThirdAdvertiserAppCode(),
                date, date, DimensionsEnum.PLATFORM);

            List<GetCustomReportDTO> list6 = oceanEngineRemoteService.getCustomReport(referralRelation.getThirdAdvertiserId(), referralRelation.getThirdAdvertiserAppCode(),
                date, date, DimensionsEnum.PROVINCE_NAME);

            List<GetCustomReportDTO> list7 = oceanEngineRemoteService.getCustomReport(referralRelation.getThirdAdvertiserId(), referralRelation.getThirdAdvertiserAppCode(),
                date, date, DimensionsEnum.CITY_NAME);

            List<GetCustomReportDTO> list8 = oceanEngineRemoteService.getCustomReport(referralRelation.getThirdAdvertiserId(), referralRelation.getThirdAdvertiserAppCode(),
                date, date, DimensionsEnum.APP_CODE);

            List<GetCustomReportDTO> list = new ArrayList<>();
            if (list1 != null && list1.size() > 0) {
                list.addAll(list1);
            }
            if (list2 != null && list2.size() > 0) {
                list.addAll(list2);
            }
            if (list3 != null && list3.size() > 0) {
                list.addAll(list3);
            }
            if (list4 != null && list4.size() > 0) {
                list.addAll(list4);
            }
            if (list5 != null && list5.size() > 0) {
                list.addAll(list5);
            }
            if (list6 != null && list6.size() > 0) {
                list.addAll(list6);
            }
            if (list7 != null && list7.size() > 0) {
                list.addAll(list7);
            }
            if (list8 != null && list8.size() > 0) {
                list.addAll(list8);
            }

            for (GetCustomReportDTO getCustomReportDTO : list) {
                OceAdvertiserDataAnalysis advertiserDataAnalysis = new OceAdvertiserDataAnalysis();
                BeanUtil.copyProperties(referralRelation, advertiserDataAnalysis);
                BeanUtil.copyProperties(getCustomReportDTO, advertiserDataAnalysis);
                addList.add(advertiserDataAnalysis);
            }

            if (addList.size() >= 500) {
                this.saveBatch(addList);
                addList = new ArrayList<>();
            }
        }
        this.saveBatch(addList);
    }

    @Override
    public void syncOceDataAnalysis() {
        OceAdvertiserReferralRelation referralRelation = oceAdvertiserReferralRelationService.getById(8683L);

        this.remove(new LambdaQueryWrapper<OceAdvertiserDataAnalysis>().eq(OceAdvertiserDataAnalysis::getThirdAdvertiserId, referralRelation.getThirdAdvertiserId())
            .eq(OceAdvertiserDataAnalysis::getThatdayTimestamp, DateTimeUtil.getTimestampSecond("2025-05-19")));

        List<GetCustomReportDTO> list1 = oceanEngineRemoteService.getCustomReport(referralRelation.getThirdAdvertiserId(), referralRelation.getThirdAdvertiserAppCode(),
            "2025-05-19", "2025-05-19", DimensionsEnum.GENDER);

        List<GetCustomReportDTO> list2 = oceanEngineRemoteService.getCustomReport(referralRelation.getThirdAdvertiserId(), referralRelation.getThirdAdvertiserAppCode(),
            "2025-05-19", "2025-05-19", DimensionsEnum.AGE);

        List<GetCustomReportDTO> list3 = oceanEngineRemoteService.getCustomReport(referralRelation.getThirdAdvertiserId(), referralRelation.getThirdAdvertiserAppCode(),
            "2025-05-19", "2025-05-19", DimensionsEnum.AGE_V2);

        List<GetCustomReportDTO> list4 = oceanEngineRemoteService.getCustomReport(referralRelation.getThirdAdvertiserId(), referralRelation.getThirdAdvertiserAppCode(),
            "2025-05-19", "2025-05-19", DimensionsEnum.AC);

        List<GetCustomReportDTO> list5 = oceanEngineRemoteService.getCustomReport(referralRelation.getThirdAdvertiserId(), referralRelation.getThirdAdvertiserAppCode(),
            "2025-05-19", "2025-05-19", DimensionsEnum.PLATFORM);

        List<GetCustomReportDTO> list6 = oceanEngineRemoteService.getCustomReport(referralRelation.getThirdAdvertiserId(), referralRelation.getThirdAdvertiserAppCode(),
            "2025-05-19", "2025-05-19", DimensionsEnum.PROVINCE_NAME);

        List<GetCustomReportDTO> list7 = oceanEngineRemoteService.getCustomReport(referralRelation.getThirdAdvertiserId(), referralRelation.getThirdAdvertiserAppCode(),
            "2025-05-19", "2025-05-19", DimensionsEnum.CITY_NAME);

        List<GetCustomReportDTO> list8 = oceanEngineRemoteService.getCustomReport(referralRelation.getThirdAdvertiserId(), referralRelation.getThirdAdvertiserAppCode(),
            "2025-05-19", "2025-05-19", DimensionsEnum.APP_CODE);

        List<GetCustomReportDTO> list = new ArrayList<>();
        list.addAll(list1);
        list.addAll(list2);
        list.addAll(list3);
        list.addAll(list4);
        list.addAll(list5);
        list.addAll(list6);
        list.addAll(list7);
        list.addAll(list8);

        List<OceAdvertiserDataAnalysis> addList = new ArrayList<>();
        for (GetCustomReportDTO getCustomReportDTO : list) {
            OceAdvertiserDataAnalysis advertiserDataAnalysis = new OceAdvertiserDataAnalysis();
            BeanUtil.copyProperties(referralRelation, advertiserDataAnalysis);
            BeanUtil.copyProperties(getCustomReportDTO, advertiserDataAnalysis);
            addList.add(advertiserDataAnalysis);
        }
        this.saveBatch(addList);
        System.out.println(1);

    }
}
